package com.psp.bu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.bu.controller.res.BaseResult;
import com.psp.bu.controller.res.ListResult;
import com.psp.bu.controller.res.ObjectResult;
import com.psp.bu.controller.res.bean.RApplySPListBean;
import com.psp.bu.controller.res.bean.RJoinedSPBean;
import com.psp.bu.controller.res.bean.RSPCommentListBean;
import com.psp.bu.controller.res.bean.RSPDetailBean;
import com.psp.bu.controller.res.bean.RSPOrderListBean;
import com.psp.bu.controller.springmvc.req.GetAgreeJoinParam;
import com.psp.bu.controller.springmvc.req.GetApplySPListParam;
import com.psp.bu.controller.springmvc.req.GetDisagreeJoinParam;
import com.psp.bu.controller.springmvc.req.GetJoinedSPParam;
import com.psp.bu.controller.springmvc.req.GetSPCommentListParam;
import com.psp.bu.controller.springmvc.req.GetSPDetailParam;
import com.psp.bu.controller.springmvc.req.GetSPOrderListParam;
import com.psp.bu.service.SPService;
import com.psp.bu.service.exception.ServiceException;
import com.psp.bu.service.impl.PageResult;

@Component
public class SPController {

	@Autowired
	SPService spServiceImpl;

	/**
	 * 获取服务商详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RSPDetailBean> detail(GetSPDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RSPDetailBean> result = new ObjectResult<>();
		String spid = param.getSpid();
		RSPDetailBean res = spServiceImpl.getDetail(spid);
		result.setData(res);
		result.setToken(param.getToken());
		return null;
	}

	/**
	 * 已经加入的服务商
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RJoinedSPBean> joined(GetJoinedSPParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RJoinedSPBean> result = new ListResult<RJoinedSPBean>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		String name = param.getName();
		String uid = "";

		PageResult<RJoinedSPBean> resList = spServiceImpl.getJoined(page, pageSize, name, uid);

		result.setTotalSize(resList.getCount());
		result.setData(resList.getData());
		result.setToken(param.getToken());
		result.setPage(page);

		return result;
	}

	/**
	 * 申请的服务商
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RApplySPListBean> getApply(GetApplySPListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RApplySPListBean> result = new ListResult<RApplySPListBean>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		String name = param.getName();
		String uid = "";

		PageResult<RApplySPListBean> resList = spServiceImpl.getApply(page, pageSize, name, uid);

		result.setTotalSize(resList.getCount());
		result.setData(resList.getData());
		result.setToken(param.getToken());
		result.setPage(page);

		return result;
	}

	/**
	 * 同意服务商的申请
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult agreeJoin(GetAgreeJoinParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();

		String spid = param.getSpid();
		String uid = "";
		try {

			boolean flag = spServiceImpl.agreeJoin(uid, spid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}

		result.setToken(param.getToken());

		return result;
	}

	/**
	 * 驳回服务商申请加入
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult disagreeJoin(GetDisagreeJoinParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String spid = param.getSpid();
		String uid = "";
		try {
			boolean flag = spServiceImpl.disagreeJoin(uid, spid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(param.getToken());
		return result;
	}

	/**
	 * 获取服务商评论
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RSPCommentListBean> comments(GetSPCommentListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RSPCommentListBean> result = new ListResult<RSPCommentListBean>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		String spid = param.getSpid();
		String uid = "";

		PageResult<RSPCommentListBean> resList = spServiceImpl.getComments(page, pageSize, uid, spid);

		result.setTotalSize(resList.getCount());
		result.setData(resList.getData());
		result.setToken(param.getToken());
		result.setPage(page);
		return result;
	}

	/**
	 * 获取服务商的订单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RSPOrderListBean> orders(GetSPOrderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RSPOrderListBean> result = new ListResult<RSPOrderListBean>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		String spid = param.getSpid();
		String uid = "";

		PageResult<RSPOrderListBean> resList = spServiceImpl.getOrders(page, pageSize, uid, spid);

		result.setTotalSize(resList.getCount());
		result.setData(resList.getData());
		result.setToken(param.getToken());
		result.setPage(page);

		return result;
	}

}
